
from microblog import My_Html_Parser
# from html.parser import HTMLParser
from html import escape
import sys, traceback, dateutil.parser
try:
    import feedgenerator
except ImportError:
    traceback.print_exc()
    def write_feed(posts, filename, params):
        pass
    print("\trss disabled - missing dependency", file=sys.stderr)
    print("\tYour microblog still may have been generated.", file=sys.stderr)

def line2words(lines, limit):
    output = []
    char_count = 0
    break_outer_loop = False
    for line in lines:
        words = line.split()
        for word in words:
            l = len(word)
            if limit > 0 and (l + char_count > limit):
                break_outer_loop = True
                break;
            output.append(word)
            char_count += l
        if break_outer_loop:
            break
    # last char of last word
    last_char = output[-1].strip()[-1]
    # print(output[-1], file=sys.stderr)
    punctuation = [".", ")", ",", "!", "?", ">", ']']
    if last_char not in punctuation:
        output.append("...")
    return output

# this is similar tot he markup function in microblog
def enrich_msg(lines, is_atom=True):
    if not is_atom:
        return string
    content = []
    parser = My_Html_Parser([])
    for line in lines:
        parser.feed(line)
        if parser.stack == [] \
        and (parser.completed_by == "" or parser.completed_by not in line):
            words = line.split()
            for i in range(len(words)):
                word  = words[i]
                if word.find("src=") == 0 \
                or word.find("href=") == 0:
                    continue
                elif word.find("https://") != -1: 
                    w = escape(word)
                    new_word = ("<a href=\"%s\">%s</a>") % (w, w)
                    words[i] = new_word
            words.insert(0,"<p>")
            words.append("</p>")
            content.append(" ".join(words))
        elif "pre" in parser.stack \
        and "code" in parser.stack \
        and "code" not in line:
            content.append(escape(line))
        else:
            content.append(line)
    return content

def write_feed(posts, filename, params, tagname=str()):
    feed = None
    if tagname != str():
        t = params["title_tagged"].format(
            __tagname__ = tagname,
            __title__ = params["title"])
        l = params["url"] + ("/tags/%s" % tagname)
    else:
        t = params["title"]
        l = params["url"]
    d = params["description"]
    if params["use_atom"]:
        feed = feedgenerator.Atom1Feed (
            title = t,
            link  = l,
            description = d,
        )
    else:
        feed = feedgenerator.DefaultFeed(
            title = t,
            link  = l,
            description = d,
        )
    base_url = l
    TITLE_LEN_LIMIT = 60
    DESC_LEN_LIMIT = -1 if params["use_atom"] else 300
    for post in posts:
        # len of post.message is number of lines
        msg = post.message
        ti = " ".join(
            line2words(msg,TITLE_LEN_LIMIT))
        if params["use_atom"]:
            de = " ".join(enrich_msg(msg))
        else:
            de = " ".join(
                line2words(msg,DESC_LEN_LIMIT))
        li = base_url + ("#%i" % post.num)
        p = dateutil.parser.parse(post.timestamp)
        if params["use_atom"]:
            feed.add_item(
                title = ti,
                link = li,
                description = str(),
                content = de,
                pubdate = p
            )
        else:
            feed.add_item(
                title = ti,
                link = li,
                description = de,
                pubdate = p
            )
    with open(filename, 'w') as f:
        f.write(feed.writeString('utf-8'))
    del feed
    return
